package com.ctsi.system.app.rest;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.system.app.rest.response.Cell;
import com.ctsi.system.app.rest.response.WorkCalendarResponse;
import com.ctsi.system.domain.DayCell;
import com.ctsi.system.interfaces.I_WorkCalendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangjw
 * @className WorkCalendarController
 * @description
 * @date 2019/3/30 13:44
 **/
@RestController
public class WorkCalendarController {
    private static final Logger log = LoggerFactory.getLogger(WorkCalendarController.class);

    @Autowired
    private I_WorkCalendar workCalendar;

    @PostMapping("/workcalendar/query")
    public UnifiedResponse<List<WorkCalendarResponse>> query(@RequestParam(value = "year" ,required = false) String year,
                                                             @RequestParam(value = "month" ,required = false) String month){
        if (UtilValidate.isEmpty(year) || UtilValidate.isEmpty(month))
        {
            return UnifiedResponse.ok(Collections.emptyList());
        }
        List<WorkCalendarResponse> week = getWeekOneMonth(Integer.parseInt(year), Integer.parseInt(month), null);
        return UnifiedResponse.ok(week);
    }


    @GetMapping("/workcalendar/query/{date}")
    public UnifiedResponse<List<WorkCalendarResponse>> queryByDate(@PathVariable(value = "date", required = true) String date){
        String[] arr = date.split("-");
        String year = arr[0];
        String month = arr[1];
        List<WorkCalendarResponse> week = getWeekOneMonth(Integer.parseInt(year), Integer.parseInt(month), null);
        return UnifiedResponse.ok(week);
    }


    public List<WorkCalendarResponse> getWeekOneMonth(Integer y, Integer m, String dateObjectId) {
        List<DayCell> list = workCalendar.getDateOneMonth(y, m);
        List<WorkCalendarResponse> week = new ArrayList<>();
        for (int i = 0; i < list.size() / 7; i++) {
            WorkCalendarResponse response = new WorkCalendarResponse();
            response.addCell(new Cell(list.get(i * 7 + 0)))
                    .addCell(new Cell(list.get(i * 7 + 1) ))
                    .addCell(new Cell(list.get(i * 7 + 2) ))
                    .addCell(new Cell(list.get(i * 7 + 3) ))
                    .addCell(new Cell(list.get(i * 7 + 4) ))
                    .addCell(new Cell(list.get(i * 7 + 5) ))
                    .addCell(new Cell(list.get(i * 7 + 6) ));
            week.add(response);
        }
        return week;
    }

    /**
     *
     * @param year
     * @param month
     * @param days
     * @return
     */
    @PostMapping("/workcalendar/save")
    public UnifiedResponse<Void> save(@RequestParam(value = "year") Integer year,
                     @RequestParam(value = "month") Integer month,
                     @RequestParam(value = "days") String days) {

        String[] day = days.split(",");
        try {
            workCalendar.save(year, month, day);
        } catch (Exception e) {
            return UnifiedResponse.error("-1","保存失败!");
        }
        return UnifiedResponse.ok();
    }

    /**
     * 创建日历
     * @param year
     * @return
     */
    @PostMapping("/workcalendar/insert")
    public UnifiedResponse<Void> insert(@RequestParam(value = "year") Integer year) {
        int[] years = workCalendar.getDefinedYears();
        for (int i = 0; i < years.length; i++) {
            if (String.valueOf(years[i]).equals(year)) {
                log.error("创建失败," + year + "年日历已存在");
                return UnifiedResponse.error("-1","创建失败," + year + "年日历已存在!");
            }
        }
        try {
            workCalendar.createWorkCalendarOneYear(year);
        } catch (Exception e) {
            log.error("创建失败", e);
            return UnifiedResponse.error("-1","创建失败!");
        }
        return UnifiedResponse.ok();
    }

}
